package com.security.door.controller.deviceManage;

import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.devDevicetype;
import com.security.door.model.dto.devDevicetypeDto;
import com.security.door.model.dto.deviceTypeDto;
import com.security.door.service.IdevDevicetypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@Api(value = "WEB - devDevicetypeController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class devDevicetypeController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IdevDevicetypeService idevDevicetypeService;

    @PostMapping("/deviceType/queryListWithPage")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper<PageInfo<devDevicetype>> queryListWithPage(@RequestBody devDevicetypeDto entityDto) {
        var list = idevDevicetypeService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping("/deviceType/selectAll")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper<List<devDevicetype>> selectAll() {
        var list = idevDevicetypeService.selectAll();
        return WrapMapper.ok(list);
    }

    @PostMapping("/deviceType/add")
    @ApiOperation(value = "新增设备类型", notes = "新增设备类型")
    public Wrapper insertDevicetype(@RequestBody devDevicetype entity) {
        if ("".equals(entity.getGuid()) || null == entity.getGuid()) {
            if (idevDevicetypeService.hasExists(entity.getTypecode()))
                return WrapMapper.error("编码已经存在");
            entity.setGuid(UUID.randomUUID().toString());
            entity.setCreatetime(new Date());
            idevDevicetypeService.save(entity);
        } else {
            entity.setCreatetime(new Date());
            idevDevicetypeService.update(entity);
        }
        return WrapMapper.ok("操作成功");
    }

    @GetMapping(value = "/deviceType/del/{guids}")
    @ApiOperation(value = "删除设备类型", notes = "删除设备类型")
    public Wrapper deleteByPrimaryKey(@PathVariable("guids") String guids) {
        idevDevicetypeService.delByTypeCode(guids);
        return WrapMapper.ok("操作成功");
    }
}
